package com.clw.controller;

import com.clw.converter.ProduceForm2ProduceDTOConverter;
import com.clw.dataobject.Produce;
import com.clw.dataobject.ProduceDetail;
import com.clw.dto.ProduceDTO;
import com.clw.enums.ResultEnum;
import com.clw.form.ProduceForm;
import com.clw.service.ProduceService;
import com.clw.utils.NumberUtils;
import com.clw.utils.ResultVOUtils;
import com.clw.utils.StringUtils;
import com.clw.vo.ResultVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import javax.validation.Valid;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2018-03-01.
 */
@Controller
@RequestMapping("produce")
public class ProduceController {
    @Autowired
    private ProduceService service;

    @RequestMapping("/list")
    public ModelAndView list(){
        return new ModelAndView("produce/index");
    }

    @ResponseBody
    @RequestMapping("getData")
    public Map<String,Object> getData(@RequestParam(value = "page", defaultValue = "1") Integer page,
                                      @RequestParam(value = "rows", defaultValue = "10") Integer rows,
                                      HttpServletRequest request) {
        PageRequest pageRequest=new PageRequest(page-1,rows);
        String proNo=request.getParameter("proNo");
        String producer=request.getParameter("producer");
        String startTime=request.getParameter("startTime");
        String endTime=request.getParameter("endTime");
        Map<String,Object> map=new HashMap<>();
        Page<Produce> producePage=service.findPage(proNo,producer,startTime,endTime,pageRequest);
        map.put("rows",producePage.getContent());
        map.put("total",service.getSize(proNo,producer,startTime,endTime));
        return map;
    }

    @RequestMapping("/view")
    public ModelAndView addView(@RequestParam(value = "id",required = false) Integer id,
                                Map<String,Object> map,HttpSession session){
        Produce produce;
        if (StringUtils.isNotEmpty(id)){
            produce=service.findOne(id);
        }
        else{
            produce=new Produce();
            produce.setProNo(NumberUtils.getMiRandom("pro"));
            Integer userId=(Integer) session.getAttribute("user_id");
            produce.setCreateUserId(userId);
        }
        map.put("produce",produce);
        return new ModelAndView("produce/add_view");
    }

    @RequestMapping("/getDetail")
    @ResponseBody
    public List<ProduceDetail> getDetail(String proNo){
        return service.getDetail(proNo);
    }

    @ResponseBody
    @RequestMapping("save")
    public ResultVO save(@Valid ProduceForm form, BindingResult bindingResult){
        if (bindingResult.hasErrors()){
            return ResultVOUtils.error(ResultEnum.PARAM_ERROR.getCode(),bindingResult.getFieldError().getDefaultMessage());
        }
        ProduceDTO produceDTO= ProduceForm2ProduceDTOConverter.convert(form);
        if (!StringUtils.isNotEmpty(form.getId())){
            service.add(produceDTO);
        }
        else {
            Produce order=service.findOne(produceDTO.getId());
            if (order.getStatus()!=0){
                return ResultVOUtils.error(ResultEnum.ORDER_STATUS_NOT_ALLOW.getCode(),
                        ResultEnum.ORDER_STATUS_NOT_ALLOW.getMessage());
            }
            service.update(produceDTO);
        }
        return ResultVOUtils.success("保存成功");
    }


    @RequestMapping("del")
    @ResponseBody
    public ResultVO del(@RequestParam(value = "id",required = true) Integer id){
        Produce produce=service.findOne(id);
        if (produce==null){
            return ResultVOUtils.error(ResultEnum.NO_FOUND.getCode(),
                    ResultEnum.NO_FOUND.getMessage());
        }
        if (produce.getStatus()!=0){
            return ResultVOUtils.error(ResultEnum.ORDER_STATUS_NOT_ALLOW.getCode(),
                    ResultEnum.ORDER_STATUS_NOT_ALLOW.getMessage());
        }
        service.del(produce);
        return ResultVOUtils.success("删除成功");
    }

    @ResponseBody
    @PostMapping("review")
    public ResultVO review(@RequestParam(value = "id",required = false) Integer id,
                           @RequestParam(value = "review",required = false) Integer review,
                           HttpSession session){
        Produce produce=service.findOne(id);
        if (produce==null){
            return ResultVOUtils.error(ResultEnum.NO_FOUND.getCode(),
                    ResultEnum.NO_FOUND.getMessage());
        }
        if (produce.getStatus()!=0){
            return ResultVOUtils.error(ResultEnum.ORDER_STATUS_NOT_ALLOW.getCode(),
                    ResultEnum.ORDER_STATUS_NOT_ALLOW.getMessage());
        }
        Integer userId=(Integer) session.getAttribute("user_id");
        if (review==-1){
            produce.setStatus(-1);
            produce.setReviewTime(new Date());
            produce.setReviewUserId(userId);
            service.save(produce);

        }
        else{
            produce.setStatus(1);
            produce.setReviewTime(new Date());
            produce.setReviewUserId(userId);
            service.review(produce);
        }
        return ResultVOUtils.success("操作成功");
    }
}
